Workflow:
knitr::opts_chunk$set(echo=T, message=F, warning=F)
library(tidyverse)
library(stringr)
library(sf)
library(geosphere)
library(mapview)
library(robis) # devtools::install_github('iobis/robis')
library(robis2) # devtools::install_github('iobis/robis2')
library(rmapshaper)
library(geojsonio)
library(rgeos)
library(scales)
library(DT)
library(leaflet)
library(htmltools)
library(htmltools)
if (basename(getwd()) != 'technical') setwd('technical')
roi = 'cmar'
roi_url = 'https://chm.cbd.int/api/v2013/documents/FB73B0D1-64FB-367F-405A-51AAA4C060F7/attachments/ETTP_8_EBSA.geojson'
#roi = 'clipperton'
#roi_url = 'https://chm.cbd.int/api/v2013/documents/494D9489-5D08-524D-84AE-AD6817304655/attachments/ETTP_2_EBSA.geojson'
roi_geo = sprintf('cache/%s.geojson', roi)
occ_csv = sprintf('cache/%s_occ.csv', roi)
occ_cklist_csv = sprintf('cache/%s_occ_checklist.csv', roi)
occ_txt = sprintf('cache/%s_occ_nwkt.txt', roi)
occ_geo = sprintf('cache/%s_occ.geojson', roi)
eez_all_rdata = 'cache/eez_all.rdata'
eez_smp_rdata = 'cache/eez_smp.rdata'
eez_geo = sprintf('cache/%s_eez.geojson', roi)
wdpa_geo = sprintf('cache/%s_wdpa.geojson', roi)
wdpa_eez_geo = sprintf('cache/%s_wdpa_eez.geojson', roi)
occ_eez_wdpa_csv = sprintf('cache/%s_occ_eez_wdpa.csv', roi)
if (!dir.exists('cache')) dir.create('cache')
obis_cols_drop = c(
'acceptedNameUsage','acceptedNameUsageID','accessRights','associatedMedia','associatedReferences','associatedSequences','associatedTaxa',
'basisOfRecord','behavior','bibliographicCitation',
'collectionID','continent','coordinatePrecision','coordinateUncertaintyInMeters','countryCode','county',
'dataGeneralizations','datasetID','dateIdentified','division','dynamicProperties',
'establishmentMeans','eventID','eventRemarks','eventTime',
'fieldNotes','fieldNumber','footprintSRS','footprintWKT','forma',
'geodeticDatum',
'habitat','higherClassification','higherGeography','higherGeographyID',
'identificationID','identificationQualifier','identificationReferences','identificationRemarks','identifiedBy','individualCount',
'individualID','informationWithheld','infraclass','infrakingdom','infraorder','infraphylum','institutionID','island','islandGroup',
'language','lifeStage','locality','locationAccordingTo','locationID','locationRemarks',
'materialSampleID','maximumDepthInMeters','minimumDepthInMeters','modified','municipality',
'nameAccordingTo','nameAccordingToID','namePublishedIn','namePublishedInID',
'occurrenceID','occurrenceRemarks','occurrenceStatus','originalNameUsage','originalNameUsageID','otherCatalogNumbers','ownerInstitutionCode',
'parvorder',
'section',
'recordNumber','recordedBy','references','reproductiveCondition','rights','rightsHolder',
'scientificNameAuthorship','scientificNameID','sex','source',
'stateProvince','subclass','subdivision','subfamily','subforma','subgenus','subkingdom','suborder',
'subphylum','subsection','subspecies','subterclass','subtribe','subvariety',
'superclass','superfamily','superorder','supertribe',
'taxonConceptID','taxonRemarks','taxonomicStatus',
'tribe','type','typeStatus',
'variety','vernacularName',
'waterBody',
'phylum','class','order','family','genus','species') # covered by checklist
Get the exclusive economic zones (EEZs) within the ROI region of interest to get at areas of political interest and management.
Fetch the entire EEZ dataset (takes ~ 2 min by hitting the MarineRegions.org WFS server at VLIZ.be) and subset based on intersecting EEZs.
if (!file.exists(eez_all_rdata)){
eez_url = 'http://geo.vliz.be/geoserver/MarineRegions/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=MarineRegions:eez'
eez_all = read_sf(eez_url) # 2.2 min
# make geometries valid
if (!is.na(sf_extSoftVersion()["lwgeom"])) {
suppressWarnings(st_is_valid(eez_all))
eez_all = st_make_valid(eez_all)
st_is_valid(eez_all)
eez_all = eez_all %>% st_cast()
}
saveRDS(eez_all, file=eez_all_rdata) # store compressed
}
eez_all = readRDS(eez_all_rdata) %>%
st_set_crs(4326)
# show table without geom
eez_all %>%
st_set_geometry(NULL)
In order to query OBIS and other web services, well known text (WKT) is needed which when passed through a URL has a limited number of characters to pass. To reduce this size, buffer by 0.1 decimal degrees and simplify the polygon (by 95%).
if (!file.exists(eez_smp_rdata)){
eez_smp = eez_all %>%
geojson_json() %>%
ms_simplify() %>%
geojson_sp() %>%
st_as_sf()
saveRDS(eez_smp, file=eez_smp_rdata) # store compressed
}
eez_smp = readRDS(eez_smp_rdata)
leaflet(eez_smp) %>%
addTiles() %>%
addPolygons()